/*
    This file is part of SQLiteGTK, written by Aly Hirani

    SQLiteGTK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SQLiteGTK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SQLiteGTK.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __UTIL_H__
#define __UTIL_H__

#include "include.h"
#include "prefs_manager.h"
#include <string.h> /* strlen */

#define BUFFER_FILE_PATH_KEY "file_path"

#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)

/* Defined here for convenience and to prevent random sizeof() everywhere */
#define g_malloc_type(type) g_malloc(sizeof(type))
#define g_malloc_types(type, count) g_malloc(sizeof(type) * count)

#ifndef DEBUG
#define D_ASSERT(x)
#else
#define D_ASSERT(x) g_assert(x)
#endif

#define R_ASSERT(x) g_assert(x)

#ifdef ENABLE_SOURCE_VIEW
extern GtkSourceLanguage *source_lang;
extern GtkSourceLanguageManager *source_lang_manager;
extern GtkSourceStyleScheme *source_style_scheme;
extern GtkSourceStyleSchemeManager *source_style_scheme_manager;
extern GRegex *hint_regex;
#endif /* ENABLE_SOURCE_VIEW */
extern guint total_windows;
extern PrefsManager *prefs_manager;
extern GtkClipboard *selection_clipboard;

enum _MessageDialogType
{
	MDT_FILE_OPEN,
	MDT_FILE_SAVE
};

typedef enum _MessageDialogType MessageDialogType;

guint voidv_length(void **array);

#ifdef VERBOSE
/**
 * Logs the given message as verbose priority. Verbose logging is only available if VERBOSE was defined at compile-time.
 * Supports standard printf syntax
 */
void log_verbose(const gchar *format, ...);

/**
 * Logs the given message as message priority. Message logging is only available if VERBOSE was defined at compile-time
 * Supports the standard printf syntax
 */
void log_message(const gchar *format, ...);
#else
#define log_verbose(x, ...)
#define log_message(x, ...)
#endif /* VERBOSE */

/**
 * Logs the given message as warning. Supports the standard printf syntax
 */
void log_warning(const gchar *format, ...);

/**
 * Logs the given message as error. Supports the standard printf syntax
 */
void log_error(const gchar *format, ...);

/**
 * A helper function to show a file dialog
 */
gchar* show_file_dialog(GtkWindow *parent_window, const gchar *title, MessageDialogType dialog_type,
	const gchar *filter_name, const gchar *filter_mimetype, const gchar *filter_pattern);

/**
 * A helper function to delete a given file. Returns TRUE if the delete was successful
 */
gboolean delete_file(const gchar *file_path);

/**
 * Returns the directory where the given pref_name should be stored.
 * @note This function currently ignores XDG/Glib implementation on Linux. This is bound
 * to change in the future very soon
 */
gchar *get_app_data_path(const gchar* pref_name);

/**
 * Initializes the internal variables of the application
 */
void init_app();

/**
 * Destroys all the initialized variables by init_app
 */
void uninit_app();

size_t g_ascii_strlen(const gchar* str);

/* TODO: WHY IS THIS IN UTILS?! */
#ifdef ENABLE_SOURCE_VIEW
/**
 * Sets the local file_path to a buffer as a "data" argument
 */
void set_buffer_file_path(GtkSourceBuffer *buffer, const gchar *file_path);

/**
 * Gets the local file_path previously set by set_buffer_file_path
 */
gchar* get_buffer_file_path(const GtkSourceBuffer *buffer);

/**
 * Saves the given buffer to disk
 */
gboolean source_buffer_save_file(GtkSourceBuffer *source_buffer);

/**
 * Loads the given buffer from disk
 */
gboolean source_buffer_open_file(GtkSourceBuffer *source_buffer, const gchar *filename);

/**
 * Creates a GtkSourceView for the editor in notebook
 */

GtkSourceView *create_gtk_source_view(const gchar *filename);

#endif /* ENABLE_SOURCE_VIEW */

/**
 * Shows a messagebox
 */
gint show_message_dialog(GtkWindow *parent, GtkDialogFlags flags, 
	GtkMessageType type, GtkButtonsType buttons, gint default_response_id, const gchar *message_format, ...);

/* TODO: WHAT IS THIS. WHY IS THIS HERE?! */
void remove_tree_view_columns_for_each(gpointer data, gpointer user_data);

guint sqlite_strv_length(const gchar **str_array);

inline gchar *strdupptr(const gchar *str, const gchar *end);

#endif /* _UTIL_H_ */
